bool polygon::Contains (const point_3d &pt) const // test the point_3d to see if it is inside the polygon
{ // begin
// based on code by Eric Haines from Graphics Gems IV
coord x, y; // indexing values
switch (plane.MajorAxis ()) // switch on the major axis of the plane_3d
{ // begin
case X: x = Y; y = Z; break; // throw away the x coordinate
case Y: x = Z; y = X; break; // throw away the y coordinate
case Z: x = X; y = Y; break; // throw away the z coordinate
} // end
real tx = pt[x], ty = pt[y]; // temporary values
point_3d *p1 = &points[count - 1], *p2 = points; // pointers to the points composing the edge being tested
int yflag0 = ((*p1)[y] >= ty), // check to see which side of the test point_3d the first point_3d is on
inside = FALSE; // start with inside false
for (int i = count; i--;) // loop over all of the points
{ // begin
int yflag1 = ((*p2)[y] >= ty); // check which side of the test point_3d the subsequent point_3d lies on
if (yflag0 != yflag1) // if the points aren't on the same side
{ // begin
int xflag0 = ((*p1)[x] >= tx); // check which side of the test coordinate the start point_3d is on
if (xflag0 == ((*p2)[x] >= tx)) // if the edge is all the way to one side of the test point_3d
{ // begin
if (xflag0) // if the start point_3d is greater than the test point_3d
inside = !inside; // increment the crossing count
} // end
else // otherwise, the edge spans the test point_3d on both axes
{ // begin
if (((*p2)[x] - ((*p2)[y] - ty) * ((*p1)[x] - (*p2)[x]) / ((*p1)[y] - (*p2)[y])) >= tx) // if the intersection of the tx axis is on the right side of the test point_3d
inside = !inside; // increment the crossing count
} // end
} // end
yflag0 = yflag1; // save the point_3d classification